﻿/**
 * Created by Administrator on 2018/3/20 0020.
 */
window.CitySearch = (function () {

    //构造器
    function CitySearch(config) {
        this.init(config);
    }
    CitySearch.prototype = {
        constructor: CitySearch,
        init: function (config) {
            var _this = this;
            _this.config = config;
            _this.trigger = document.querySelector(config.trigger);
            _this.PageBody();

        },
        //构建组件主体
        PageBody: function () {
            var _this = this;
            var forHtml = '';
            var data = citydata[100000];

            if (data != null) {
                var sb = "";
                var letter = "";
                $.each(data, function (i, name) {
                    var selected = "";
                    if (i == _this.config.pid)
                    {
                        selected = "active";
                    }
                    forHtml += ' <li class="item ' + selected + '"  pid="' + i + '">' + name + '</li>'
                })
                var CitySearchDiv = "<div class='bmcw-search-address'>"
                                  + '<div class="bmcw-search-address-pro clearfix" id="ProvinceList">'
                                  + '     <ul>' + forHtml + ''
                                  + '          </ul></div>'
                                  + ' <div class="bmcw-search-address-city clearfix" id="CityList" style="display: none;"></div></div>';
                _this.trigger.innerHTML = CitySearchDiv;

                $("#ProvinceList").find("li").each(function () {
                    $(this).click(function () {
                        if ($(this).attr("pid") != null) {
                            $("#ProvinceList").find("li").each(function () {
                                $(this).removeClass("active")
                            });
                            $(this).addClass("active");
                            _this.config.callback($(this).attr("pid"));
                        }
                    });
                    
                });

            }

        }
    };
    return CitySearch;
})();